package com.ccc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ccc.dao.StockinfoDao;
import com.ccc.model.Product;
import com.ccc.model.Role;
import com.ccc.model.Stockinfo;
import com.ccc.model.User;
import com.ccc.util.dao.UtilDao;

@Component("stockinfoDao")
public class StockinfoDaoImpl implements StockinfoDao {
	private UtilDao utilDao;

	@Override
	public void addStockinfo(Stockinfo stockinfo) {
		utilDao.save(stockinfo);
	}

	@Override
	public void deleteStockinfo(Stockinfo stockinfo) {
		utilDao.delete(stockinfo);
	}

	@Override
	public void updateStockinfo(Stockinfo stockinfo) {
		utilDao.update(stockinfo);
	}

	@Override
	public Stockinfo findStockinfo(int id) {
		return utilDao.find(Stockinfo.class, id);
	}
	
	@Override
	public Stockinfo findByUserProductTime(User user, Product product, Integer currentTime){
		List<Stockinfo> list = utilDao.find(
				"from Stockinfo s where s.user = ? and s.product = ? and s.currentTime = ?",
				new Object[]{user,product,currentTime});
		if(list==null||list.size()==0)
			return null;
		return list.get(0);
	}

	public UtilDao getUtilDao() {
		return utilDao;
	}

	@Resource
	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}
	
	@Override
	public List<User> findDistinctUserByTime(Role role,Integer currentTime) {
		return utilDao.find("select distinct s.user from Stockinfo s where s.user.role=? and s.currentTime = ?", new Object[]{role,currentTime});
	}

	@Override
	public List<Product> findDistinctProductByUserAndTime(User user,Integer currentTime) {
		return utilDao.find("select distinct s.product from Stockinfo s where s.user=? and s.currentTime = ?", new Object[]{user,currentTime});
	}
}
